package com.huanletao.guava.str;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class JoinerDemo {
    private final List<String> stringList = Arrays.asList(
            "tom","jack","bob");

    private final List<String> stringListWithNullValue = Arrays.asList(
            "tom","jack","bob",null,"rose");

    @Test
    public void testJoin(){
        String join = Joiner.on("#").join(stringList);
        System.out.println("join = " + join);
    }

    // 跳过list中的null
    @Test
    public void testJoinOnJoinSkipNull(){
        String join = Joiner.on("#").skipNulls().join(stringListWithNullValue);
        System.out.println("join = " + join);
    }

    // 替换null为指定字符
    @Test
    public void testJoinOnJoinUseForNull(){
        String join = Joiner.on("#").useForNull("NULL").join(stringListWithNullValue);
        System.out.println("join = " + join);
    }

    // appendTo
    @Test
    public void testJoinOnJoinAppendTo(){
        final StringBuilder buffer = new StringBuilder("TEST");
        StringBuilder buffer_new = Joiner.on("#").appendTo(buffer, stringList);
        System.out.println("join = " + buffer_new);
    }

    private final Map<String,String> stringMap = ImmutableMap.of("a","b","c","d");

    /**
     * 对Map kv拆分
     */
    @Test
    public void testJoinOnWithMap(){
        String join = Joiner.on("#").withKeyValueSeparator("=").join(stringMap);
        System.out.println("join = " + join);
    }

}
